package vue;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

import modele.Manga;

public class MangaFrm extends JDialog {
	
	/**
	 * ID de la form
	 */
	private static final long serialVersionUID = 1L;
    
    /**
     * Objet courant
     */
	Manga manga;
    
    /**
     * Vrai si la fenêtre a fermé avec le bouton Sauvegarder
     */
    boolean sauvegarde;

	private final JPanel contentPanel = new JPanel();
	private final JButton btnEnregistrer = new JButton("Enregistrer");
	private final JButton btnAnnuler = new JButton("Annuler");
	private JLabel lblImage = new JLabel("");
	private JTextField textField;
	
	/**
     * Affiche la fenêtre pour créer un nouveau Auteur
     * @param bd BD courante pour ajouter le nouveau Auteur
     * @return Vrai si l'ajout a fonctionné
     */
    public Manga creer() {
        this.manga = new Manga();
        
        this.setVisible(true);
        
        if (this.sauvegarde == true) {
        }
        return this.manga;
    }
    
    /**
     * Affiche la fenêtre pour modifier le Auteur
     * @param bd BD courante
     * @param Auteur Auteur a modifier
     * @return Vrai si la modification a fonctionné
     */
    public boolean modifier(Manga objet) {
        this.manga = objet;
        
        this.setVisible(true);
        
        if (this.sauvegarde == true) {
        }
        return this.sauvegarde;
    }

	/**
	 * Create the dialog.
	 */
	public MangaFrm() {
		setModal(true);
		setTitle("Manga");
		setBounds(100, 100, 578, 293);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		btnEnregistrer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sauvegarde = true;
		        dispose();
			}
		});
		btnEnregistrer.setBounds(12, 169, 117, 25);
		
		contentPanel.add(btnEnregistrer);
		btnAnnuler.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sauvegarde = false;
		        dispose();
			}
		});
		btnAnnuler.setForeground(Color.RED);
		btnAnnuler.setBounds(12, 225, 117, 25);
		
		contentPanel.add(btnAnnuler);
		
		contentPanel.add(lblImage);
		
		JSpinner spinner = new JSpinner();
		spinner.setModel(new SpinnerNumberModel(new Integer(0), new Integer(0), null, new Integer(1)));
		spinner.setBounds(414, 47, 150, 20);
		contentPanel.add(spinner);
		
		JLabel lblNumro = new JLabel("Numéro");
		lblNumro.setBounds(276, 49, 70, 15);
		contentPanel.add(lblNumro);
		
		JLabel lblSrie = new JLabel("Série");
		lblSrie.setFont(new Font("Dialog", Font.BOLD, 24));
		lblSrie.setBounds(12, 12, 105, 25);
		contentPanel.add(lblSrie);
		
		JLabel lblTitre = new JLabel("Titre");
		lblTitre.setBounds(12, 45, 100, 15);
		contentPanel.add(lblTitre);
		
		JLabel lblAuteur = new JLabel("Auteur");
		lblAuteur.setBounds(12, 60, 100, 15);
		contentPanel.add(lblAuteur);
		
		JLabel lblDessinateur = new JLabel("Dessinateur");
		lblDessinateur.setBounds(12, 75, 100, 15);
		contentPanel.add(lblDessinateur);
		
		JLabel lblgeConseill = new JLabel("Âge conseillé");
		lblgeConseill.setBounds(12, 90, 100, 15);
		contentPanel.add(lblgeConseill);
		
		JLabel lbl_serie_titre = new JLabel("Titre");
		lbl_serie_titre.setBounds(125, 45, 70, 15);
		contentPanel.add(lbl_serie_titre);
		
		JLabel lbl_serie_auteur = new JLabel("Auteur");
		lbl_serie_auteur.setBounds(125, 60, 100, 15);
		contentPanel.add(lbl_serie_auteur);
		
		JLabel lbl_serie_dessinateur = new JLabel("Dessinateur");
		lbl_serie_dessinateur.setBounds(125, 75, 100, 15);
		contentPanel.add(lbl_serie_dessinateur);
		
		JLabel lbl_serie_age = new JLabel("Âge conseillé");
		lbl_serie_age.setBounds(125, 90, 100, 15);
		contentPanel.add(lbl_serie_age);
		
		JLabel lblVolume = new JLabel("Volume");
		lblVolume.setFont(new Font("Dialog", Font.BOLD, 24));
		lblVolume.setBounds(276, 12, 117, 25);
		contentPanel.add(lblVolume);
		
		JLabel lblDateDeParution = new JLabel("Date de parution");
		lblDateDeParution.setBounds(276, 76, 132, 15);
		contentPanel.add(lblDateDeParution);
		
		JSpinner spinner_1 = new JSpinner();
		spinner_1.setBounds(414, 101, 150, 20);
		contentPanel.add(spinner_1);
		
		JLabel lblNbDePages = new JLabel("Nb de pages");
		lblNbDePages.setBounds(276, 103, 117, 15);
		contentPanel.add(lblNbDePages);
		
		textField = new JTextField();
		textField.setBounds(414, 128, 150, 19);
		contentPanel.add(textField);
		textField.setColumns(10);
		
		JLabel lblCodeEan = new JLabel("Code EAN");
		lblCodeEan.setBounds(276, 128, 87, 15);
		contentPanel.add(lblCodeEan);
		
		JTextArea textArea = new JTextArea();
		textArea.setBounds(414, 155, 150, 65);
		contentPanel.add(textArea);
		
		JLabel lblDescription = new JLabel("Description");
		lblDescription.setBounds(276, 155, 87, 15);
		contentPanel.add(lblDescription);
		
		final Canvas canvasImage = new Canvas();
		canvasImage.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				FileFilter images = new FileNameExtensionFilter("images", "png", "jpg");
				fileChooser.setAcceptAllFileFilterUsed(false);
				fileChooser.addChoosableFileFilter(images);
				int ret = fileChooser.showDialog(null, "Open file");

			    if (ret == JFileChooser.APPROVE_OPTION) {
			    	File file = fileChooser.getSelectedFile();
			    	try {
						Image image = ImageIO.read(file);
				    	Graphics g = canvasImage.getGraphics();
				    	g.drawImage(image, 0, 0, 88, 130, Color.white, new ImageObserver() {
							
							@Override
							public boolean imageUpdate(Image img, int infoflags, int x, int y,
									int width, int height) {
								// TODO Auto-generated method stub
								return false;
							}
						});
					} catch (IOException e1) {
						e1.printStackTrace();
					}
			    	
			    }
			}
		});
		canvasImage.setBackground(Color.BLACK);
		canvasImage.setBounds(157, 120, 88, 130);
		contentPanel.add(canvasImage);
		
		JSpinner spinner_2 = new JSpinner();
		spinner_2.setModel(new SpinnerDateModel(new Date(1345262400000L), null, null, Calendar.DAY_OF_YEAR));
		spinner_2.setEditor(new JSpinner.DateEditor(spinner_2, "dd-MM-yyyy"));
		spinner_2.setBounds(414, 75, 150, 20);
		contentPanel.add(spinner_2);
	}
}
